<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/28
 * Time: 13:52
 */

namespace Home\Logic;


use Home\Model\Page;

/**
 * 领取记录
 * Class OrderLogic
 * @package Home\Logic
 */
class OrderLogic
{
    /**
     * 分页条件查询
     * @param $map 查询条件
     * @param $pagenumber 页号
     * @param $pagesize 页面大小
     * @return mixed
     */
    public function getByConditionPage($map, $pagenumber, $pagesize)
    {
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = D('Order')->join('LEFT JOIN t_goods g ON g.id=t_order.goods_id')
            ->where($map)->limit($limit)->order('t_order.id desc')
            ->field('g.title,t_order.*')
            ->select();
        foreach ($list as $key=>$value){
            $list[$key]['create_time'] = date('Y/m/d H:i',$value['create_time']);
        }
        $data['list'] = $list;
        $count = D('Order')->where($map)->count();
        $page = new Page($pagenumber, $count, $pagesize);
        $page = (array)$page;//转换为数组
        $data['page'] = $page;
        return $data;
    }


    /**
     * 添加订单
     * @param $goods array 充值卡
     * @param $user_id int 用户id
     * @param $model object
     */
    public function addOrder($goods,$user_id,$model){
        $log['money'] = $goods['money'];
        $log['poundage'] = $goods['poundage'];
        $log['total_money'] = $goods['money'] * 10 + $goods['poundage'];
        $log['user_id'] = $user_id;
        $log['goods_id'] = $goods['id'];
        $log['status'] = 0;
        $log['create_time'] = time();
        $model->table('t_order')->add($log);
    }
}